package com.ruoyi.web.controller.task;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.task.domain.SysUserTask;
import com.ruoyi.task.service.ISysUserTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户任务Controller
 * 
 * @author ruoyi
 * @date 2022-06-23
 */
@RestController
@RequestMapping("/task/personlTask")
public class SysUserTaskController extends BaseController
{
    @Autowired
    private ISysUserTaskService sysUserTaskService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 根据单位等级查询用户任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:personlTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserTask sysUserTask) {
        Long userId = SecurityUtils.getUserId();
        if (SysUser.isAdmin(userId)) {
        } else if (UserType.CONSERVATOR.getValue().equals(SecurityUtils.getUserType())) {
            // 管理员显示所有本级以下的内容
            Long deptId = SecurityUtils.getDeptId();
            List<Long> deptIds = deptService.selectNormalDeptById(deptId);
            if (StringUtils.isEmpty(deptIds)) {
                return null;
            }
            sysUserTask.setDeptIds(deptIds);
        } else {
            sysUserTask.setUserId(userId);
        }

        startPage();
        List<SysUserTask> list = sysUserTaskService.selectSysUserTaskList(sysUserTask);
        return getDataTable(list);
    }

    /**
     * 查询用户任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:personlTask:list')")
    @GetMapping("/taskList")
    public TableDataInfo taskList(SysUserTask sysUserTask) {
        startPage();
        List<SysUserTask> list = sysUserTaskService.selectSysUserTaskList(sysUserTask);
        return getDataTable(list);
    }

    /**
     * 前端-获取用户任务列表
     * @param sysUserTask
     * @return
     */
    @GetMapping("/apiList")
    public TableDataInfo apiList(SysUserTask sysUserTask) {
        startPage();
        List<SysUserTask> list = sysUserTaskService.selectApiUserTaskList(sysUserTask);
        return getDataTable(list);
    }


    /**
     * 前端-获取用户已创建任务列表
     * @param sysUserTask
     * @return
     */
    @GetMapping("/taskCreateList")
    public TableDataInfo taskCreateList(SysUserTask sysUserTask) {
        startPage();
        List<SysUserTask> list = sysUserTaskService.taskCreateList(sysUserTask);
        return getDataTable(list);
    }


    /**
     * 获取视频流
     * @param taskId 用户任务ID
     * @return  视频流信息
     */
    @GetMapping(value = "/rtsp/{taskId}")
    public AjaxResult getRtsp(@PathVariable("taskId") String taskId) throws Exception {
        return AjaxResult.success(sysUserTaskService.getRtsp(taskId));
    }

    /**
     * 前端-通过任务编号获取任务的参与人员列表
     * @param sysUserTask
     * @return
     */
    @PostMapping("/personList")
    public AjaxResult personList(@RequestBody SysUserTask sysUserTask)
    {
        return AjaxResult.success(sysUserTaskService.personList(sysUserTask));
    }

    /**
     * 前端-更新任务参与人员准备状态
     * @param
     * @return
     */
    @PostMapping("/updateReady")
    public AjaxResult updateReady(@RequestParam(value = "readyStatus") String readyStatus,@RequestParam(value = "taskNo") String taskNo)
    {
        return AjaxResult.success(sysUserTaskService.updateReady(readyStatus,taskNo));
    }
    /**
     * 导出用户任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:personlTask:export')")
    @Log(title = "用户任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserTask sysUserTask)
    {
        List<SysUserTask> list = sysUserTaskService.selectSysUserTaskList(sysUserTask);
        ExcelUtil<SysUserTask> util = new ExcelUtil<SysUserTask>(SysUserTask.class);
        util.exportExcel(response, list, "用户任务数据");
    }

    /**
     * 获取用户任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('task:personlTask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysUserTaskService.selectSysUserTaskById(id));
    }

    /**
     * 新增用户任务
     */
    @PreAuthorize("@ss.hasPermi('task:personlTask:add')")
    @Log(title = "用户任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserTask sysUserTask)
    {
        return toAjax(sysUserTaskService.insertSysUserTask(sysUserTask));
    }

    /**
     * 修改用户任务
     */
    @PreAuthorize("@ss.hasPermi('task:personlTask:edit')")
    @Log(title = "用户任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysUserTask sysUserTask)
    {
        return toAjax(sysUserTaskService.updateSysUserTask(sysUserTask));
    }

    /**
     * 前端-修改用户任务
     */
    @Log(title = "前端-修改用户任务", businessType = BusinessType.UPDATE)
    @PostMapping("/updateUserTask")
    public AjaxResult updateUserTask(@RequestBody SysUserTask sysUserTask)
    {
        return toAjax(sysUserTaskService.updateUserTask(sysUserTask));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('task:personlTask:edit')")
    @Log(title = "个人任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/changeUserTaskStatus")
    public AjaxResult changeUserTaskStatus(@RequestBody SysUserTask sysUserTask)
    {
        return toAjax(sysUserTaskService.updateUserTaskStatus(sysUserTask));
    }

    /**
     * 根据ID集合删除用户任务
     *
     */
    @PreAuthorize("@ss.hasPermi('task:personlTask:remove')")
    @Log(title = "用户任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysUserTaskService.deleteSysUserTaskByIds(ids));
    }

    /**
     * 删除用户任务
     *
     */
    @PreAuthorize("@ss.hasPermi('task:personlTask:remove')")
    @Log(title = "删除用户任务", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody SysUserTask userTask)
    {
        return toAjax(sysUserTaskService.deleteUserTask(userTask));
    }
}
